from django import forms
from operation.models import *
import re
# 原来我们这样写form，但下面直接继承了models
# class UserAskForm(forms.Form):
#     name = forms.CharField(required=True,min_length=2,max_length=50)
#     phone = forms.CharField(required=True,min_length=11,max_length=11)
#     course_name = forms.CharField(required=True,max_length=100,min_length=4)

class UserAskForm(forms.ModelForm):
    class Meta:
        model = UserAsk
        fields = ['name','mobile','course_name']

    # 验证手机号是否合法
    def clean_mobile(self):
        mobile = self.cleaned_data['mobile']
        REGEX_MOBILE = "^1[358]\d{9}$|^147\d{8}$|^176\d{8}$"
        p = re.compile(REGEX_MOBILE)
        if p.match(mobile):
            return mobile
        else:
            raise forms.ValidationError('手机号码不对！',code='mobile_invalid')
